﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Web.Security;

namespace TireService.Web.UserControls
{
    public partial class ucTireSetLog : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void callbackTireSetLog_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int TireSetId)
        {
            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            // Load form data
            gvTireSetLog.DataSource = db.GetTireSetLog(TireSetId).OrderBy(p => p.LogDate);
            gvTireSetLog.DataBind();
        }

        /// <summary>
        /// Gridview function that change field data for unbound fields
        /// </summary>
        protected void gvTireSetLog_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            // Format UnBoundColumn FromTireSetStatusDescription: Show from tireset status description
            if (e.Column.FieldName == "FromTireSetStatusDescription")
            {
                // Get tiresetstatusid
                int FromTireSetStatusId = Convert.ToInt32(e.GetListSourceFieldValue("FromTireSetStatusId"));

                TireServiceDB db = new TireServiceDB();

                e.Value = db.GetTireSetStatusDescription(FromTireSetStatusId);
            }

            // Format UnBoundColumn ToTireSetStatusDescription: Show to tireset status description
            if (e.Column.FieldName == "ToTireSetStatusDescription")
            {
                // Get tiresetstatusid
                int ToTireSetStatusId = Convert.ToInt32(e.GetListSourceFieldValue("ToTireSetStatusId"));

                TireServiceDB db = new TireServiceDB();

                e.Value = db.GetTireSetStatusDescription(ToTireSetStatusId);
            }

            // Format UnBoundColumn UserName: Show username
            if (e.Column.FieldName == "UserName")
            {
                // Get userid
                string UserId = e.GetListSourceFieldValue("UserId").ToString();

                e.Value = Membership.GetUser(new Guid(UserId)).UserName;
            }

            // Format UnBoundColumn ContainerCargoRoom: Show container and cargoroom
            if (e.Column.FieldName == "ContainerCargoRoom")
            {
                // Get ContainerCargoRoomId
                int? ContainerCargoRoomId = Convert.ToInt32(e.GetListSourceFieldValue("ContainerCargoRoomId"));

                if (ContainerCargoRoomId != null)
	            {
		            TireServiceDB db = new TireServiceDB();

                    ContainerCargoRoom cargoroom = db.GetContainerCargoRoom(Convert.ToInt32(ContainerCargoRoomId));
                    Container container = db.GetContainer(cargoroom.ContainerId);

                    e.Value = container.ContainerNumber + " / " + cargoroom.CargoRoomNumber.ToString();
                }
                else
                {
                    e.Value = "";
                }
            }
        }
    }
}